package com.yt.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: mxli
 * @Date: 2021-06-11 01:28
 **/
public class DesensitizationUtils {

    /**
     * 脱敏规则——手机号码(手机号码隐藏中间4位数)
     *
     * @param str
     * @return
     */
    public static String phoneNum(String str) {
        if (isEmpty(str)) {
            return "";
        }

        String rtnStr = str;
        if (isEmpty(rtnStr) || rtnStr.trim().length() < 7) {
            return rtnStr;
        }
        rtnStr = rtnStr.trim();
        // 校验是否为电话号码
        String regExp = "^1\\d{10}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(rtnStr);
        if (m.matches()) {// 若判断为固话
//        	rtnStr = replace(rtnStr,rtnStr.length()-3,"****");
            rtnStr = replace(rtnStr, 4, "****");
        }
        return rtnStr;
    }

    /**
     * 脱敏规则——邮箱 (@前三位)
     *
     * @param str
     * @return
     */
    public static String emailNum(String str) {
        if (isEmpty(str)) {
            return "";
        }
        String rtnStr = str;
        String regExp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        Pattern p = Pattern.compile(regExp);
        Matcher matcher = p.matcher(rtnStr);
        boolean isMatched = matcher.matches();
        if (isMatched) {
            int s = rtnStr.indexOf("@");
            if (s >= 6) {
                return replace(rtnStr.trim(), s - 3, "****");
            } else if (s >= 3 && s < 6) {
                return replace(rtnStr.trim(), s - 1, "**");
            } else {
                return str;
            }
        } else {
            return rtnStr;
        }
    }

    /**
     * 脱敏规则——邮箱 (@前三位)
     *
     * @param str
     * @return
     */
    public static String nameNum(String str) {
        return replace(str, 1, "*");
    }


    /**
     * 脱敏规则
     *
     * @param str     str(需脱敏的字符串)
     * @param n       n(脱敏开始位)
     * @param newChar newChar(脱敏掩码)
     * @return
     */
    public static String replace(String str, int n, String newChar) {
        if (isEmpty(str)) {
            return "";
        }
        String s1 = "";
        String s2 = "";
        try {
            s1 = str.substring(0, n - 1);
            s2 = str.substring(n - 1 + newChar.length());
            return s1 + newChar + s2;
        } catch (Exception ex) {
            return str;
        }
    }

    /**
     * 判断字符串是否为null || 空字符串
     *
     * @param param
     * @return
     */
    public static boolean isEmpty(String param) {
        return param == null || "".equals(param.trim());
    }

    /**
     * 脱敏中间
     *
     * @param str
     * @return
     */
    public static String replace(String str) {
        if (isEmpty(str)) {
            return "";
        }

        int length = str.length();
        int nums = length / 2;
        if (length < 4) {
            return replace(str, 1, "*");
        }
        int beginIndex = nums - (nums / 2);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < nums; i++) {
            sb.append("*");
        }

        return replace(str, beginIndex, sb.toString());
    }

    public static void main(String[] args) {
        String str = "13628478730";
        System.out.println(phoneNum("13693048545"));
        System.out.println(emailNum("adfafds@qq.com"));
        System.out.println(replace("张三", 1, "*"));
        System.out.println(replace("成都市郫都区ldskfjlkasjf撒酒疯立刻就上课了"));
        System.out.println(replace("张三1"));
    }

}
